{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "name": "разбор ДЗ 7.ipynb",
      "provenance": []
    },
    "kernelspec": {
      "display_name": "Python 3 (ipykernel)",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.8.12"
    }
  },
  "cells": [
    {
      "cell_type": "code",
      "metadata": {
        "ExecuteTime": {
          "end_time": "2019-09-21T11:54:21.177758Z",
          "start_time": "2019-09-21T11:54:21.010827Z"
        },
        "id": "v1MDkR7GBdvi"
      },
      "source": [
        "import numpy as np\n",
        "import scipy.linalg"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "AZ7tifYIBdwS"
      },
      "source": [
        "#### __1.__ Решить систему уравнений методом Крамера:\n",
        "\n",
        "   а) $\\begin{cases}\n",
        "x_{1}-2x_{2}=1 \\\\\n",
        "3x_{1}-4x_{2}=7\n",
        "\\end{cases}$\n",
        "    \n",
        "   б) $\\begin{cases}\n",
        "2x_{1}-x_{2}+5x_{3}=10 \\\\\n",
        "x_{1}+x_{2}-3x_{3}=-2 \\\\\n",
        "2x_{1}+4x_{2}+x_{3}=1\n",
        "\\end{cases}$"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "ExecuteTime": {
          "end_time": "2019-09-21T11:54:21.196644Z",
          "start_time": "2019-09-21T11:54:21.179686Z"
        },
        "id": "Xu-NkpIpBdwW",
        "outputId": "b0e848e2-4381-4891-d00a-dd61c3c587da"
      },
      "source": [
        "# а)\n",
        "\n",
        "A = np.array([[1, -2], [3, -4]])\n",
        "b = np.array([1, 7])\n",
        "\n",
        "det_A = np.linalg.det(A)\n",
        "\n",
        "print(f'A:\\n{A}\\n')\n",
        "print(f'b:\\n{b}\\n')\n",
        "\n",
        "print(f'det(A): {det_A}\\n')\n",
        "\n",
        "if det_A != 0:\n",
        "    print(f'Определитель матрицы квадратной системы не равен 0, система совместна и имеет единственное решение.\\n')\n",
        "\n",
        "A1 = np.column_stack([b, A[:, 1]])\n",
        "det_A1 = np.linalg.det(A1)\n",
        "\n",
        "print(f'A1:\\n{A1}\\n')\n",
        "print(f'det(A1): {det_A1}\\n')\n",
        "\n",
        "A2 = np.column_stack([A[:, 0], b])\n",
        "det_A2 = np.linalg.det(A2)\n",
        "\n",
        "print(f'A2:\\n{A2}\\n')\n",
        "print(f'det(A2): {det_A2}\\n')\n",
        "\n",
        "print(f'x1 = {det_A1 / det_A}')\n",
        "print(f'x2 = {det_A2 / det_A}')"
      ],
      "execution_count": null,
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "A:\n",
            "[[ 1 -2]\n",
            " [ 3 -4]]\n",
            "\n",
            "b:\n",
            "[1 7]\n",
            "\n",
            "det(A): 2.0000000000000004\n",
            "\n",
            "Определитель матрицы квадратной системы не равен 0, система совместна и имеет единственное решение.\n",
            "\n",
            "A1:\n",
            "[[ 1 -2]\n",
            " [ 7 -4]]\n",
            "\n",
            "det(A1): 9.999999999999998\n",
            "\n",
            "A2:\n",
            "[[1 1]\n",
            " [3 7]]\n",
            "\n",
            "det(A2): 4.0\n",
            "\n",
            "x1 = 4.999999999999998\n",
            "x2 = 1.9999999999999996\n"
          ]
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "ExecuteTime": {
          "end_time": "2019-09-21T11:54:21.217584Z",
          "start_time": "2019-09-21T11:54:21.200630Z"
        },
        "id": "g-oviySNBdwi",
        "outputId": "313f5221-5e89-42ac-d851-44290867ab97"
      },
      "source": [
        "# б)\n",
        "\n",
        "A = np.array([[2, -1, 5], [1, 1, -3], [2, 4, 1]])\n",
        "b = np.array([10, -2, 1])\n",
        "\n",
        "det_A = np.linalg.det(A)\n",
        "\n",
        "print(f'A:\\n{A}\\n')\n",
        "print(f'b:\\n{b}\\n')\n",
        "\n",
        "print(f'det(A): {det_A}\\n')\n",
        "\n",
        "if det_A != 0:\n",
        "    print(f'Определитель матрицы квадратной системы не равен 0, система совместна и имеет единственное решение.\\n')\n",
        "\n",
        "A1 = np.column_stack([b, A[:, 1:]])\n",
        "det_A1 = np.linalg.det(A1)\n",
        "\n",
        "print(f'A1:\\n{A1}\\n')\n",
        "print(f'det(A1): {det_A1}\\n')\n",
        "\n",
        "A2 = np.column_stack([A[:, 0], b, A[:, 2]])\n",
        "det_A2 = np.linalg.det(A2)\n",
        "\n",
        "print(f'A2:\\n{A2}\\n')\n",
        "print(f'det(A2): {det_A2}\\n')\n",
        "\n",
        "A3 = np.column_stack([A[:, 0:2], b])\n",
        "det_A3 = np.linalg.det(A3)\n",
        "\n",
        "print(f'A3:\\n{A3}\\n')\n",
        "print(f'det(A3): {det_A3}\\n')\n",
        "\n",
        "print(f'x1 = {det_A1 / det_A}')\n",
        "print(f'x2 = {det_A2 / det_A}')\n",
        "print(f'x3 = {det_A3 / det_A}')"
      ],
      "execution_count": null,
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "A:\n",
            "[[ 2 -1  5]\n",
            " [ 1  1 -3]\n",
            " [ 2  4  1]]\n",
            "\n",
            "b:\n",
            "[10 -2  1]\n",
            "\n",
            "det(A): 42.99999999999998\n",
            "\n",
            "Определитель матрицы квадратной системы не равен 0, система совместна и имеет единственное решение.\n",
            "\n",
            "A1:\n",
            "[[10 -1  5]\n",
            " [-2  1 -3]\n",
            " [ 1  4  1]]\n",
            "\n",
            "det(A1): 86.00000000000004\n",
            "\n",
            "A2:\n",
            "[[ 2 10  5]\n",
            " [ 1 -2 -3]\n",
            " [ 2  1  1]]\n",
            "\n",
            "det(A2): -43.000000000000014\n",
            "\n",
            "A3:\n",
            "[[ 2 -1 10]\n",
            " [ 1  1 -2]\n",
            " [ 2  4  1]]\n",
            "\n",
            "det(A3): 42.99999999999998\n",
            "\n",
            "x1 = 2.0000000000000018\n",
            "x2 = -1.0000000000000009\n",
            "x3 = 1.0\n"
          ]
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "CrivgYVGBdwm"
      },
      "source": [
        "#### __2.__ Найти $L$-матрицу $LU$-разложения для матрицы коэффициентов:\n",
        "\n",
        "   а)$$\\begin{pmatrix}\n",
        "1 & 2 & 4 \\\\ \n",
        "2 & 9 & 12 \\\\ \n",
        "3 & 26 & 30\n",
        "\\end{pmatrix}$$\n",
        "    \n",
        "   б)$$\\begin{pmatrix}\n",
        "1 & 1 & 2 & 4\\\\ \n",
        "2 & 5 & 8 & 9\\\\ \n",
        "3 & 18 & 29 & 18\\\\\n",
        "4 & 22 & 53 & 33\n",
        "\\end{pmatrix}$$"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "0MzKITXFBdwp"
      },
      "source": [
        "а) Матрица коэффициентов:\n",
        "\n",
        "### $$\\begin{pmatrix}\n",
        "1 & 2 & 4 \\\\ \n",
        "2 & 9 & 12 \\\\ \n",
        "3 & 26 & 30\n",
        "\\end{pmatrix}$$\n",
        "\n",
        "Умножим 1 строку на 2 и вычтем из 2 строки:\n",
        "\n",
        "Матрица U:<br>\n",
        "\n",
        "### $$\\begin{pmatrix}\n",
        "1 & 2 & 4 \\\\ \n",
        "0 & 5 & 4 \\\\ \n",
        "3 & 26 & 30\n",
        "\\end{pmatrix}$$\n",
        "\n",
        "Матрица L:\n",
        "\n",
        "### $$\\begin{pmatrix}\n",
        "1 & 0 & 0 \\\\ \n",
        "2 & 1 & 0 \\\\ \n",
        "I_{31} & I_{32} & 1\n",
        "\\end{pmatrix}$$\n",
        "\n",
        "Умножим 1 строку на 3 и вычтем из 3 строки:\n",
        "\n",
        "Матрица U:\n",
        "\n",
        "### $$\\begin{pmatrix}\n",
        "1 & 2 & 4 \\\\ \n",
        "0 & 5 & 4 \\\\ \n",
        "0 & 20 & 18\n",
        "\\end{pmatrix}$$\n",
        "\n",
        "Матрица L:\n",
        "\n",
        "### $$\\begin{pmatrix}\n",
        "1 & 0 & 0 \\\\ \n",
        "2 & 1 & 0 \\\\ \n",
        "3 & I_{32} & 1\n",
        "\\end{pmatrix}$$\n",
        "\n",
        "Умножим 2 строку на 4 и вычтем из 3 строки:\n",
        "\n",
        "Матрица U:\n",
        "\n",
        "### $$\\begin{pmatrix}\n",
        "1 & 2 & 4 \\\\ \n",
        "0 & 5 & 4 \\\\ \n",
        "0 & 0 & 2\n",
        "\\end{pmatrix}$$\n",
        "\n",
        "Матрица L:\n",
        "\n",
        "### $$\\begin{pmatrix}\n",
        "1 & 0 & 0 \\\\ \n",
        "2 & 1 & 0 \\\\ \n",
        "3 & 4 & 1\n",
        "\\end{pmatrix}$$"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "ExecuteTime": {
          "end_time": "2019-09-21T11:54:21.231586Z",
          "start_time": "2019-09-21T11:54:21.220576Z"
        },
        "id": "lBH0dsjvBdwu",
        "outputId": "0958b0e2-3c40-40b7-fb5a-b8cf282a26ed"
      },
      "source": [
        "A = np.array([[1, 2, 4], [2, 9, 12], [3, 26, 30]])\n",
        "U = np.array([[1, 2, 4], [0, 5, 4], [0, 0, 2]])\n",
        "L = np.array([[1, 0, 0], [2, 1, 0], [3, 4, 1]])\n",
        "\n",
        "LU = np.dot(L, U)\n",
        "\n",
        "print(f'A:\\n{A}\\n')\n",
        "print(f'L:\\n{L}\\n')\n",
        "print(f'U:\\n{U}\\n')\n",
        "print(f'LU:\\n{LU}\\n')"
      ],
      "execution_count": null,
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "A:\n",
            "[[ 1  2  4]\n",
            " [ 2  9 12]\n",
            " [ 3 26 30]]\n",
            "\n",
            "L:\n",
            "[[1 0 0]\n",
            " [2 1 0]\n",
            " [3 4 1]]\n",
            "\n",
            "U:\n",
            "[[1 2 4]\n",
            " [0 5 4]\n",
            " [0 0 2]]\n",
            "\n",
            "LU:\n",
            "[[ 1  2  4]\n",
            " [ 2  9 12]\n",
            " [ 3 26 30]]\n",
            "\n"
          ]
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "kdv9FiBZBdwx"
      },
      "source": [
        "б) Матрица коэффициентов:\n",
        "\n",
        "### $$\\begin{pmatrix}\n",
        "1 & 1 & 2 & 4 \\\\ \n",
        "2 & 5 & 8 & 9 \\\\ \n",
        "3 & 18 & 29 & 18 \\\\\n",
        "4 & 22 & 53 & 33\n",
        "\\end{pmatrix}$$\n",
        "\n",
        "Умножим 1 строку на 2 и вычтем из 2 строки:\n",
        "\n",
        "Матрица U:\n",
        "\n",
        "### $$\\begin{pmatrix}\n",
        "1 & 1 & 2 & 4 \\\\ \n",
        "0 & 3 & 4 & 1 \\\\ \n",
        "3 & 18 & 29 & 18 \\\\\n",
        "4 & 22 & 53 & 33\n",
        "\\end{pmatrix}$$\n",
        "\n",
        "Матрица L:\n",
        "\n",
        "### $$\\begin{pmatrix}\n",
        "1 & 0 & 0 & 0 \\\\ \n",
        "2 & 1 & 0 & 0 \\\\ \n",
        "I_{31} & I_{32} & 1 & 0 \\\\\n",
        "I_{41} & I_{42} & I_{43} & 1\n",
        "\\end{pmatrix}$$\n",
        "\n",
        "Умножим 1 строку на 3 и вычтем из 3 строки:\n",
        "\n",
        "Матрица U:\n",
        "\n",
        "### $$\\begin{pmatrix}\n",
        "1 & 1 & 2 & 4 \\\\ \n",
        "0 & 3 & 4 & 1 \\\\ \n",
        "0 & 15 & 23 & 6 \\\\\n",
        "4 & 22 & 53 & 33\n",
        "\\end{pmatrix}$$\n",
        "\n",
        "Матрица L:\n",
        "\n",
        "### $$\\begin{pmatrix}\n",
        "1 & 0 & 0 & 0 \\\\ \n",
        "2 & 1 & 0 & 0 \\\\ \n",
        "3 & I_{32} & 1 & 0 \\\\\n",
        "I_{41} & I_{42} & I_{43} & 1\n",
        "\\end{pmatrix}$$\n",
        "\n",
        "Умножим 1 строку на 4 и вычтем из 4 строки:\n",
        "\n",
        "Матрица U:\n",
        "\n",
        "### $$\\begin{pmatrix}\n",
        "1 & 1 & 2 & 4 \\\\ \n",
        "0 & 3 & 4 & 1 \\\\ \n",
        "0 & 15 & 23 & 6 \\\\\n",
        "0 & 18 & 45 & 17\n",
        "\\end{pmatrix}$$\n",
        "\n",
        "Матрица L:\n",
        "\n",
        "### $$\\begin{pmatrix}\n",
        "1 & 0 & 0 & 0 \\\\ \n",
        "2 & 1 & 0 & 0 \\\\ \n",
        "3 & I_{32} & 1 & 0 \\\\\n",
        "4 & I_{42} & I_{43} & 1\n",
        "\\end{pmatrix}$$\n",
        "\n",
        "Умножим 2 строку на 5 и вычтем из 3 строки:\n",
        "\n",
        "Матрица U:\n",
        "\n",
        "### $$\\begin{pmatrix}\n",
        "1 & 1 & 2 & 4 \\\\ \n",
        "0 & 3 & 4 & 1 \\\\ \n",
        "0 & 0 & 3 & 1 \\\\\n",
        "0 & 18 & 45 & 17\n",
        "\\end{pmatrix}$$\n",
        "\n",
        "Матрица L:\n",
        "\n",
        "### $$\\begin{pmatrix}\n",
        "1 & 0 & 0 & 0 \\\\ \n",
        "2 & 1 & 0 & 0 \\\\ \n",
        "3 & 5 & 1 & 0 \\\\\n",
        "4 & I_{42} & I_{43} & 1\n",
        "\\end{pmatrix}$$\n",
        "\n",
        "Умножим 2 строку на 6 и вычтем из 4 строки:\n",
        "\n",
        "Матрица U:\n",
        "\n",
        "### $$\\begin{pmatrix}\n",
        "1 & 1 & 2 & 4 \\\\ \n",
        "0 & 3 & 4 & 1 \\\\ \n",
        "0 & 0 & 3 & 1 \\\\\n",
        "0 & 0 & 21 & 11\n",
        "\\end{pmatrix}$$\n",
        "\n",
        "Матрица L:\n",
        "\n",
        "### $$\\begin{pmatrix}\n",
        "1 & 0 & 0 & 0 \\\\ \n",
        "2 & 1 & 0 & 0 \\\\ \n",
        "3 & 5 & 1 & 0 \\\\\n",
        "4 & 6 & I_{43} & 1\n",
        "\\end{pmatrix}$$\n",
        "\n",
        "Умножим 3 строку на 7 и вычтем из 4 строки:\n",
        "\n",
        "Матрица U:\n",
        "\n",
        "### $$\\begin{pmatrix}\n",
        "1 & 1 & 2 & 4 \\\\ \n",
        "0 & 3 & 4 & 1 \\\\ \n",
        "0 & 0 & 3 & 1 \\\\\n",
        "0 & 0 & 0 & 4\n",
        "\\end{pmatrix}$$\n",
        "\n",
        "Матрица L:\n",
        "\n",
        "### $$\\begin{pmatrix}\n",
        "1 & 0 & 0 & 0 \\\\ \n",
        "2 & 1 & 0 & 0 \\\\ \n",
        "3 & 5 & 1 & 0 \\\\\n",
        "4 & 6 & 7 & 1\n",
        "\\end{pmatrix}$$"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "ExecuteTime": {
          "end_time": "2019-09-21T11:54:21.244513Z",
          "start_time": "2019-09-21T11:54:21.233543Z"
        },
        "id": "aIshti6_Bdw3",
        "outputId": "4b1ea894-e3ee-4350-a4b3-2c4a5a776f23"
      },
      "source": [
        "A = np.array([[1, 1, 2, 4], [2, 5, 8, 9], [3, 18, 29, 18], [4, 22, 53, 33]])\n",
        "U = np.array([[1, 1, 2, 4], [0, 3, 4, 1], [0, 0, 3, 1], [0, 0, 0, 4]])\n",
        "L = np.array([[1, 0, 0, 0], [2, 1, 0, 0], [3, 5, 1, 0], [4, 6, 7, 1]])\n",
        "\n",
        "LU = np.dot(L, U)\n",
        "\n",
        "print(f'A:\\n{A}\\n')\n",
        "print(f'L:\\n{L}\\n')\n",
        "print(f'U:\\n{U}\\n')\n",
        "print(f'LU:\\n{LU}\\n')"
      ],
      "execution_count": null,
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "A:\n",
            "[[ 1  1  2  4]\n",
            " [ 2  5  8  9]\n",
            " [ 3 18 29 18]\n",
            " [ 4 22 53 33]]\n",
            "\n",
            "L:\n",
            "[[1 0 0 0]\n",
            " [2 1 0 0]\n",
            " [3 5 1 0]\n",
            " [4 6 7 1]]\n",
            "\n",
            "U:\n",
            "[[1 1 2 4]\n",
            " [0 3 4 1]\n",
            " [0 0 3 1]\n",
            " [0 0 0 4]]\n",
            "\n",
            "LU:\n",
            "[[ 1  1  2  4]\n",
            " [ 2  5  8  9]\n",
            " [ 3 18 29 18]\n",
            " [ 4 22 53 33]]\n",
            "\n"
          ]
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "kOix8yCpBdw8"
      },
      "source": [
        "#### __3.__ Решить систему линейных уравнений методом $LU$-разложения\n",
        "\n",
        "$$\\begin{cases}\n",
        "2x_{1}+x_{2}+3x_{3}=1 \\\\\n",
        "11x_{1}+7x_{2}+5x_{3}=-6 \\\\\n",
        "9x_{1}+8x_{2}+4x_{3}=-5\n",
        "\\end{cases}$$"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "ExecuteTime": {
          "end_time": "2019-09-21T11:54:21.260470Z",
          "start_time": "2019-09-21T11:54:21.246507Z"
        },
        "id": "O0BT6nbeBdw_",
        "outputId": "22db2ced-27ad-4e8a-fb4d-eb44debb04e7"
      },
      "source": [
        "A = np.array([[2, 1, 3], [11, 7, 5], [9, 8, 4]])\n",
        "b = np.array([1, -6, -5])\n",
        "\n",
        "lu, piv = scipy.linalg.lu_factor(A)\n",
        "x = scipy.linalg.lu_solve((lu, piv), b)\n",
        "\n",
        "print(f'A:\\n{A}\\n')\n",
        "print(f'b:\\n{b}\\n')\n",
        "print(f'x:\\n{x}\\n')\n",
        "print(f'Ax:\\n{A @ x}\\n')\n",
        "print(f'Ax - b:\\n{A @ x - b}\\n')\n",
        "\n",
        "print(f'lu:\\n{lu}\\n')\n",
        "print(f'piv:\\n{piv}\\n')"
      ],
      "execution_count": null,
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "A:\n",
            "[[ 2  1  3]\n",
            " [11  7  5]\n",
            " [ 9  8  4]]\n",
            "\n",
            "b:\n",
            "[ 1 -6 -5]\n",
            "\n",
            "x:\n",
            "[-1.00000000e+00  2.44249065e-16  1.00000000e+00]\n",
            "\n",
            "Ax:\n",
            "[ 1. -6. -5.]\n",
            "\n",
            "Ax - b:\n",
            "[0. 0. 0.]\n",
            "\n",
            "lu:\n",
            "[[11.          7.          5.        ]\n",
            " [ 0.81818182  2.27272727 -0.09090909]\n",
            " [ 0.18181818 -0.12        2.08      ]]\n",
            "\n",
            "piv:\n",
            "[1 2 2]\n",
            "\n"
          ]
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "r4BHMn6PBdxA"
      },
      "source": [
        "### $$\\begin{pmatrix}\n",
        "\\left.\\begin{matrix}\n",
        "2 & 1 & 3\\\\ \n",
        "11 & 7 & 5\\\\ \n",
        "9 & 8 & 4\n",
        "\\end{matrix}\n",
        "\\right|\\begin{matrix}\n",
        "1\\\\ \n",
        "-6\\\\\n",
        "-5\n",
        "\\end{matrix}\n",
        "\\end{pmatrix}\n",
        "\\,\\,\\,\\,\\,\\Rightarrow \\,\\,\\,\\,\\,\n",
        "\\begin{pmatrix}\n",
        "\\left.\\begin{matrix}\n",
        "2 & 1 & 3\\\\ \n",
        "22 & 14 & 10\\\\ \n",
        "18 & 16 & 8\n",
        "\\end{matrix}\\right|\n",
        "\\begin{matrix}\n",
        "1\\\\ \n",
        "-12\\\\\n",
        "-10\n",
        "\\end{matrix}\n",
        "\\end{pmatrix}$$\n",
        "\n",
        "### $$\\begin{pmatrix}\n",
        "2 & 1 & 3 \\\\ \n",
        "22 & 14 & 10 \\\\ \n",
        "18 & 16 & 8\n",
        "\\end{pmatrix} \n",
        "\\,\\,\\,\\,\\,\\Rightarrow \\,\\,\\,\\,\\,\n",
        "\\begin{pmatrix}\n",
        "2 & 1 & 3 \\\\ \n",
        "0 & 3 & -23 \\\\ \n",
        "0 & 7 & -19\n",
        "\\end{pmatrix}\n",
        "\\,\\,\\,\\,\\,\\Rightarrow \\,\\,\\,\\,\\,\n",
        "\\begin{pmatrix}\n",
        "2 & 1 & 3 \\\\ \n",
        "0 & 3 & -23 \\\\ \n",
        "0 & 0 & \\frac{104}{3}\n",
        "\\end{pmatrix}=U$$\n",
        "### $$\\begin{pmatrix}\n",
        "1 & 0 & 0 \\\\ \n",
        "l_{21} & 1 & 0 \\\\ \n",
        "l_{31} & l_{32} & 1\n",
        "\\end{pmatrix} \n",
        "\\,\\,\\,\\,\\,\\Rightarrow \\,\\,\\,\\,\\,\n",
        "\\begin{pmatrix}\n",
        "1 & 0 & 0 \\\\ \n",
        "11 & 1 & 0 \\\\ \n",
        "9 & l_{32} & 1\n",
        "\\end{pmatrix}\n",
        "\\,\\,\\,\\,\\,\\Rightarrow \\,\\,\\,\\,\\,\n",
        "\\begin{pmatrix}\n",
        "1 & 0 & 0 \\\\ \n",
        "11 & 1 & 0 \\\\ \n",
        "9 & \\frac{7}{3} & 1\n",
        "\\end{pmatrix}=L$$\n",
        "\n",
        "Решим систему $Ly=b$:\n",
        "\n",
        "### $$\\begin{cases}\n",
        "y_1=1 \\\\\n",
        "11y_{1}+y_2=-12 \\\\\n",
        "9y_{1}+\\frac{7}{3}y_{2}+y_{3}=-10\n",
        "\\end{cases}\n",
        "\\,\\,\\,\\,\\,\\Rightarrow \\,\\,\\,\\,\\,\n",
        "\\begin{cases}\n",
        "y_1=1 \\\\\n",
        "y_2=-23 \\\\\n",
        "y_{3}=\\frac{104}{3}\n",
        "\\end{cases}$$\n",
        "\n",
        "Решим систему $Ux=y$:\n",
        "### $$\\begin{cases}\n",
        "2x_1+x_2+3x_3=1 \\\\\n",
        "3x_2-23x_3=-23 \\\\\n",
        "\\frac{104}{3}x_3=\\frac{104}{3}\n",
        "\\end{cases}\n",
        "\\,\\,\\,\\,\\,\\Rightarrow \\,\\,\\,\\,\\,\n",
        "\\begin{cases}\n",
        "x_1=-1 \\\\\n",
        "x_2=0 \\\\\n",
        "x_3=1\n",
        "\\end{cases}$$\n",
        "Ответ: $(-1,0,1)$"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "F8dy_YrRBdxC"
      },
      "source": [
        "#### __4.__ Решить систему линейных уравнений методом Холецкого\n",
        "\n",
        "$$\\begin{cases}\n",
        "81x_{1}-45x_{2}+45x_{3}=531 \\\\\n",
        "-45x_{1}+50x_{2}-15x_{3}=-460 \\\\\n",
        "45x_{1}-15x_{2}+38x_{3}=193\n",
        "\\end{cases}$$"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "ExecuteTime": {
          "end_time": "2019-09-21T11:54:21.278423Z",
          "start_time": "2019-09-21T11:54:21.262466Z"
        },
        "id": "C4-SsfL4BdxD",
        "outputId": "d5e46a5d-3dc5-4ec7-f3d9-f1ca964e15fc"
      },
      "source": [
        "A = np.array([[81, -45, 45], [-45, 50, -15], [45, -15, 38]])\n",
        "b = np.array([531, -460, 193])\n",
        "\n",
        "K = scipy.linalg.cho_factor(A)\n",
        "x = scipy.linalg.cho_solve(K, b)\n",
        "\n",
        "print(f'A:\\n{A}\\n')\n",
        "print(f'K:\\n{K}\\n')\n",
        "print(f'b:\\n{b}\\n')\n",
        "print(f'x:\\n{x}\\n')\n",
        "print(f'Ax:\\n{A @ x}\\n')\n",
        "print(f'Ax - b:\\n{A @ x - b}\\n')"
      ],
      "execution_count": null,
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "A:\n",
            "[[ 81 -45  45]\n",
            " [-45  50 -15]\n",
            " [ 45 -15  38]]\n",
            "\n",
            "K:\n",
            "(array([[  9.,  -5.,   5.],\n",
            "       [-45.,   5.,   2.],\n",
            "       [ 45., -15.,   3.]]), False)\n",
            "\n",
            "b:\n",
            "[ 531 -460  193]\n",
            "\n",
            "x:\n",
            "[ 6. -5. -4.]\n",
            "\n",
            "Ax:\n",
            "[ 531. -460.  193.]\n",
            "\n",
            "Ax - b:\n",
            "[0. 0. 0.]\n",
            "\n"
          ]
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "5rSfAb2WBdxF"
      },
      "source": [
        "### $$l_{11}=\\sqrt{a_{11}}=\\sqrt{81}=9,$$\n",
        "### $$l_{21}=\\frac{a_{21}}{l_{11}}=\\frac{-45}{9}=-5,$$\n",
        "### $$l_{31}=\\frac{a_{31}}{l_{11}}=\\frac{45}{9}=5,$$\n",
        "### $$l_{22}=\\sqrt{a_{22}-l_{21}^{2}}=\\sqrt{50 - 25}=\\sqrt{25}=5,$$\n",
        "### $$l_{32}=\\frac{1}{l_{22}}\\left ( a_{32}-l_{21}l_{31} \\right)=\\frac{1}{5}(-15+25)=\\frac{10}{5}=2,$$\n",
        "### $$l_{33}=\\sqrt{a_{33}-l_{31}^{2}-l_{32}^{2}}=\\sqrt{38-25-4}=\\sqrt{9}=3.$$\n",
        "Получили матрицу \n",
        "$L = \\begin{pmatrix}\n",
        "9 & 0 & 0 \\\\ \n",
        "-5 & 5 & 0 \\\\ \n",
        "5 & 2 & 3\n",
        "\\end{pmatrix}, \n",
        "\\; \\;\n",
        "L^{T} = \\begin{pmatrix}\n",
        "9 & -5 & 5 \\\\ \n",
        "0 & 5 & 2 \\\\ \n",
        "0 & 0 & 3\n",
        "\\end{pmatrix}.$\n",
        "### $$\\begin{cases}\n",
        "9y_1=531, \\\\\n",
        "-5y_1+5y_2=-460, \\\\\n",
        "5y_1+2y_2+3y_3=193.\n",
        "\\end{cases} \n",
        "\\,\\,\\,\\,\\,\\Rightarrow \\,\\,\\,\\,\\,\n",
        "\\begin{cases}\n",
        "y_1=59, \\\\\n",
        "y_2=-33, \\\\\n",
        "y_3=-12.\n",
        "\\end{cases}$$\n",
        "### $$\\begin{cases}\n",
        "9x_1-5x_2+5x_3=59, \\\\\n",
        "5x_2+2x_3=-33, \\\\\n",
        "3x_{3}=-12.\n",
        "\\end{cases}\n",
        "\\,\\,\\,\\,\\,\\Rightarrow \\,\\,\\,\\,\\,\n",
        "\\begin{cases}\n",
        "x_1=6, \\\\\n",
        "x_2=-5, \\\\\n",
        "x_3=-4.\n",
        "\\end{cases}$$\n",
        "Ответ: $(6,-5,-4)$"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "v7Q8S5gQBdxH"
      },
      "source": [
        "#### __5*.__ Написать на Python программу с реализацией одного из изученных алгоритмов решения СЛАУ."
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "ExecuteTime": {
          "end_time": "2019-09-21T11:54:21.301360Z",
          "start_time": "2019-09-21T11:54:21.282412Z"
        },
        "id": "zsbxb3wsBdxI"
      },
      "source": [
        "def LU(A): \n",
        "  \n",
        "    n = len(A)\n",
        "    \n",
        "    L = [[0 for _ in range(n)] for _ in range(n)]\n",
        "    U = [[0 for _ in range(n)] for _ in range(n)] \n",
        "                  \n",
        "    for i in range(n):\n",
        "        for k in range(i, n):\n",
        "            s = 0; \n",
        "            \n",
        "            for j in range(i): \n",
        "                s += (L[i][j] * U[j][k]) \n",
        "  \n",
        "            U[i][k] = A[i][k] - s\n",
        "  \n",
        "        for k in range(i, n): \n",
        "            if (i == k):\n",
        "                L[i][i] = 1 \n",
        "            else:\n",
        "                s = 0\n",
        "                \n",
        "                for j in range(i): \n",
        "                    s += (L[k][j] * U[j][i])\n",
        "  \n",
        "                L[k][i] = ((A[k][i] - s) / U[i][i]) \n",
        "  \n",
        "    return L, U"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "ExecuteTime": {
          "end_time": "2019-09-21T11:54:21.317317Z",
          "start_time": "2019-09-21T11:54:21.305349Z"
        },
        "id": "b_jZJXG7BdxO",
        "outputId": "55624114-f99a-4a26-b329-b3351aa5c28b"
      },
      "source": [
        "A = np.array([[2, 1, 3], [11, 7, 5], [9, 8, 4]])\n",
        "#A = np.array([[1, 2, 3], [7, 11, 5], [8, 9, 4]])\n",
        "b = np.array([1, -6, -5])\n",
        "L, U = LU(A)\n",
        "\n",
        "\n",
        "\n",
        "print(f'A:\\n{A}\\n')\n",
        "print(f'b:\\n{b}\\n')\n",
        "print(f'L:\\n{np.array(L)}\\n')\n",
        "print(f'U:\\n{np.array(U)}\\n')\n",
        "print(f'LU:\\n{np.array(L) @ np.array(U)}\\n')\n",
        "y = np.linalg.inv(np.array(L)) @ b\n",
        "print(f'y:\\n{y}\\n')\n",
        "\n",
        "x = np.linalg.inv(np.array(U)) @ y\n",
        "print(f'x:\\n{x}\\n')"
      ],
      "execution_count": null,
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "A:\n",
            "[[ 2  1  3]\n",
            " [11  7  5]\n",
            " [ 9  8  4]]\n",
            "\n",
            "b:\n",
            "[ 1 -6 -5]\n",
            "\n",
            "L:\n",
            "[[1.         0.         0.        ]\n",
            " [5.5        1.         0.        ]\n",
            " [4.5        2.33333333 1.        ]]\n",
            "\n",
            "U:\n",
            "[[  2.           1.           3.        ]\n",
            " [  0.           1.5        -11.5       ]\n",
            " [  0.           0.          17.33333333]]\n",
            "\n",
            "LU:\n",
            "[[ 2.  1.  3.]\n",
            " [11.  7.  5.]\n",
            " [ 9.  8.  4.]]\n",
            "\n",
            "y:\n",
            "[  1.         -11.5         17.33333333]\n",
            "\n",
            "x:\n",
            "[-1.0000000e+00 -8.8817842e-16  1.0000000e+00]\n",
            "\n"
          ]
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "DL_zGv0vBdxY"
      },
      "source": [
        ""
      ],
      "execution_count": null,
      "outputs": []
    }
  ]
}